<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JLoader::register('VariableHelper', AUTOGENERATOR_DIR_ROOT . '/helpers/variable.php');

class AutogeneratorViewComponent extends JViewLegacy {

    protected $form;
    protected $item;
    protected $state;
    protected $sContent;

    /**
     *
     * @var AutogeneratorModelFields 
     */
    public $oModelFields;

    public function display($tpl = null) {
        $this->form = $this->get('Form');
        $this->item = $this->get('Item');
        $this->state = $this->get('State');

        $this->addToolbar();
        
        parent::display($tpl);
    }

    protected function addToolbar() {
        $oApp = JFactory::getApplication();
        $oApp->input->set('hidemainmenu', true);
        $sLayout = $oApp->input->get('layout');
        $sFileName = $oApp->input->get('file');


        if ($sLayout == 'edit') {
            JToolbarHelper::apply('component.apply');
            JToolbarHelper::save('component.save');
            JToolbarHelper::save2new('component.save2new');
        } elseif ($sLayout == 'review') {
            $oUser = JFactory::getUser();

            switch ($sFileName) {
                case 'install.mysql.utf8.sql':
                    $this->sContent = AutogeneratorHelper::getSqlInstallContent($this->item->id);
                    break;

                case 'uninstall.mysql.utf8.sql':
                    $this->sContent = AutogeneratorHelper::getSqlUninstallContent($this->item->id);
                    break;

                case 'helper.php':
                    $this->sContent = AutogeneratorHelper::getHelperContent($this->item->id);
                    break;

                default:
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_COMPONENT'] = $this->item->title;
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR'] = $oUser->name;
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_CREATE_DATE'] = JFactory::getDate()->format('F o');
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_COPYRIGHT'] = $this->item->copyright;
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_EMAIL'] = $oUser->email;
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_AUTHOR_URL'] = 'www.joomla.org';
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_VERSION'] = '3.0.0';
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_INCLUDE_HEADER_TEMPLATE'] = '';
                    // Get default views.
                    $aTypeListMVCs = AutogeneratorHelper::getAllMVCsByComponentId($this->item->id, ConstantHelper::$_MVC_TYPE_LIST);
                    VariableHelper::$_VARIABLES['AUTO_GENERATOR_DEFAULT_VIEWS'] = isset($aTypeListMVCs[0]->title) ? $aTypeListMVCs[0]->title : '';

                    $this->sContent = AutogeneratorHelper::getContentFileInTemplate($sFileName, $oApp->input->get('type'), $oApp->input->getBool('bIsAdmin', true), $oApp->input->getBool('bIsMain', false));
                    break;
            }
        }
        
        JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_COMPONENT_DETAILS'), 'article.png');
        JToolbarHelper::cancel('component.cancel');
    }

}
